/**
 * app消息通知
 * Author: 6小新
 * Creation Date: 2024-09-23
 * Description:
 */

interface NotificationInfo<T> {
  title: string
  body?: string
  icon?: string
  tag?: string
  data?: T
}

interface CustomNotification<T> extends Notification {
  data: T
}

/**
 *
 * @param info
 * @param callback
 */

export default function appNotification<T>(
  info: NotificationInfo<T>,
  callback?: (e: Event & { target: CustomNotification<T> }) => void
) {
  const notification = new window.Notification(info.title, {
    body: info.body,
    icon: info.icon,
    tag: info.tag,
    data: info.data
  }) as CustomNotification<T>

  notification.onclick = (e: Event) => {
    if (callback) callback(e as Event & { target: CustomNotification<T> })
  }
}
